// 竞态
import { Subject, switchMap, concatMap, mergeMap, from } from "rxjs";

function fetchData(id) {
  return from(
    new Promise((resolve) => {
      setTimeout(() => resolve(id), 1000 * id);
    })
  );
}

const search = new Subject();
// 只会拿到 1 search.next(3); 会产生 Observable1,
// search.next(1);又会产生 Observable2,
// Observable1会被switchMap，干掉，所以 只会拿到 1

search.pipe(switchMap(fetchData)).subscribe(console.log);
// 会拿到 3 1
search.pipe(concatMap(fetchData)).subscribe(console.log);

search.next(3);

setTimeout(() => {
  search.next(1);
}, 1000);
